package com.ming.controller;

import com.ming.pojo.LastSevenCount;
import com.ming.pojo.Sum;
import com.ming.service.CountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
public class CountController {

    @Autowired
    private CountService lastSevenCountService;

    @Autowired
    private CountService countService;

    @GetMapping("/getByEnterTime")
    public int[] getByEnterTime(){

        int[] enterTimes = new int[7];

        List<LastSevenCount> byEnterTime = lastSevenCountService.getByEnterTime();

        for (LastSevenCount lastSevenCount : byEnterTime) {

            Date date = new Date(2022,lastSevenCount.getMonth()-1, lastSevenCount.getDay()-1);
            int day = date.getDay();

            int i = 6 - ((new Date().getDay() - day)+7)%7;

            enterTimes[i] = lastSevenCount.getCounts();
        }

        return enterTimes;
    }

    @GetMapping("/getByLeaveTime")
    public int[] getByLeaveTime(){

        int[] leaveTimes = new int[7];

        List<LastSevenCount> byLeaveTime = lastSevenCountService.getByLeaveTime();

        for (LastSevenCount lastSevenCount : byLeaveTime) {

            Date date = new Date(2022,lastSevenCount.getMonth()-1, lastSevenCount.getDay()-1);
            int day = date.getDay();

            int i = 6 - ((new Date().getDay() - day)+7)%7;

            leaveTimes[i] = lastSevenCount.getCounts();
        }

        return leaveTimes;
    }

    @GetMapping("/getTypeCount")
    public List<Integer> getTypeCount() {
        return countService.getTypeCount();
    }

    @GetMapping("/getSum")
    public Sum getSum() {
        System.out.println(countService.getSum());
        return countService.getSum();
    }

}
